/**
 * Ceicei.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package com.ceicei.taobaoke.api.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ceicei.project.dal.daointerface.CeiceiItemDAO;
import com.ceicei.project.dal.daointerface.CeiceiItemsPriceLogDAO;
import com.ceicei.project.dal.daointerface.CeiceiPromotionLogDAO;
import com.ceicei.util.CeiceiConstants;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;

/**
 * 淘宝客户端调用基础类
 *
 * @author 李威-liwei2672@gmail.com
 * @version $Id: BaseRequestTest.java, v 0.1 2012-4-18 下午04:06:32 李威 Exp $
 */
public class BaseTaoApiRequestTest {
    private static Log                       logger = LogFactory
                                                        .getLog(BaseTaoApiRequestTest.class);

    // 正式环境需要设置为:
    protected TaobaoClient                   client;

    protected ClassPathXmlApplicationContext appContext;

    protected CeiceiItemDAO                  ceiceiItemDAO;

    protected CeiceiItemsPriceLogDAO         ceiceiItemsPriceLogDAO;

    protected CeiceiPromotionLogDAO          ceiceiPromotionLogDAO;

    /**
     * 初始化
     *
     */
    public void init() {

        //构造淘宝API客户端
        client = new DefaultTaobaoClient(CeiceiConstants.url, CeiceiConstants.appkey,
            CeiceiConstants.appSecret);

        //加载spring容器，
        appContext = new ClassPathXmlApplicationContext(new String[] { "ibatis-spring.xml" });

        ceiceiItemDAO = (CeiceiItemDAO) appContext.getBean("ceiceiItemDAO");

        ceiceiItemsPriceLogDAO = (CeiceiItemsPriceLogDAO) appContext
            .getBean("ceiceiItemsPriceLogDAO");

        ceiceiPromotionLogDAO = (CeiceiPromotionLogDAO) appContext.getBean("ceiceiPromotionLogDAO");
    }

    /**
     * 线程睡眠
     *
     * @param time
     */
    protected void waitSomeTime(int time) {
        try {
            Thread.sleep(time);
        } catch (Exception e) {
            logger.error("调用淘宝API的线程-睡眠时发生异常", e);
        }
    }

    public TaobaoClient getClient() {
        return client;
    }

    public CeiceiItemDAO getCeiceiItemDAO() {
        return ceiceiItemDAO;
    }

    public CeiceiItemsPriceLogDAO getCeiceiItemsPriceLogDAO() {
        return ceiceiItemsPriceLogDAO;
    }

    public CeiceiPromotionLogDAO getCeiceiPromotionLogDAO() {
        return ceiceiPromotionLogDAO;
    }

}
